package com.lry.rpc.netty.client;

import com.lry.exception.FrameworkException;
import com.lry.rpc.config.NettyClientConfig;
import com.lry.rpc.netty.Disposable;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;


@Slf4j
public class RpcClient implements Disposable, RemotingClient {

    protected ThreadPoolExecutor connectExecutor;

    private NettyClientConfig nettyClientConfig;

    private AtomicBoolean init = new AtomicBoolean(false);

    private RpcClientBootstrap bootstrap;

    public RpcClient(NettyClientConfig nettyClientConfig){
        this.nettyClientConfig = nettyClientConfig;
    }

    public RpcClient(){
        this(new NettyClientConfig());
    }


    @Override
    public void start() {
        if(init.get()){
            log.warn("rpc client already starting....");
            return;
        }

        init.compareAndSet(false,true);

        this.bootstrap = new RpcClientBootstrap(nettyClientConfig);

        this.connectExecutor = new ThreadPoolExecutor(5,
                10, 10, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>());

        connectServer();
    }

    public void connectServer(){
        InetSocketAddress address = InetSocketAddress.createUnresolved(nettyClientConfig.getHost(),nettyClientConfig.getPort());
        Channel channel = bootstrap.connect(address);
    }

    @Override
    public void shutdown() {
        destroy();
    }

    @Override
    public void destroy() {

    }

    public static void main(String[] args) {
        RpcClient rpcClient = new RpcClient();
        rpcClient.start();
    }


}
